public class LinkedList<E> {

    private class Node{
        public E e;

        public Node next;



        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }


        public Node(E e){
            this(e,null);
        }

        public Node(Node next){
            this(null,next);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString(){
            return e.toString();
        }
    }

    private Node head;

    private int size;
    public void LinkedList(){
        size = 0;
        head = null;
    }

    // 获取联表长度
    public int getSize(){
        return size;
    }

    // 判断联表是否为空
    public boolean isEmpty(){
        return size == 0;
    }

    // 向链表头添加元素
    public void addFirst(E e){
        head = new Node(e, head);
        size ++ ;
    }

    // 向联表之间添加元素
    public void add(E e , int index) throws Exception {
        checkSize(index);
        if (index ==0){
            this.addFirst(e);
        }else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
//                Node node  = new Node(e);
//                node.next = prev.next;
//                prev.next  = node;


            prev.next = new Node(e,prev.next);

            size ++ ;
        }
    }

    public void checkSize(int index) throws Exception {
        if (index < 0 || index > size){
            throw new Exception("index不合法");
        }
    }

    public void addLast(E e) throws Exception {
        this.add(e,size);

    }


}
